#!/usr/bin/env bash
#
# author:yang
# script name:sub_install_zk.sh
# description: 用于安装自动安装zk脚本,可以配合parent_zk_kafka_install.sh执行，也可以单独执行
source /etc/profile

#curl -o zookeeper-3.4.14.tar.gz http://172.18.19.208/soft/zookeeper/zookeeper-3.4.14.tar.gz
DOWNLOAD_URL=http://172.18.19.208/soft/zookeeper/zookeeper-3.4.14.tar.gz
DOWNLOAD_DIR=/usr/local/src
INSTALL_DIR=/usr/local
ZK_FILE=$(echo $DOWNLOAD_URL|awk -F'/' '{print $NF}')
ZK_DIR=$(echo ${ZK_FILE}|awk -F'.t' '{print $1}') 
LINK=/usr/local/zookeeper
INSTALL_USER=root
RUN_USER=zookeeper
CURRENT_TIME=$(date +%F-%H:%M:%S)
LOG_DIR=/tmp/install_zk_${CURRENT_TIME}.log


# function mk_dir() {
#     data_dirs=($(echo $1|tr ' ,' ' '))

#     for data_dir in ${data_dirs};do
#         [ -d ${data_dir} ]||mkdir -p ${data_dir}||{ echo "$(date +"%F %H:%M:%S") [ERROR] 创建${data_dir}失败，请检查.";exit 30; }
#         [ $? -eq 0 ]&& chown -R ${RUN_USER}:${RUN_USER} ${data_dir}
#     done;
# }



function zk_install() {
     # 判断下载目录是否存在，如果不存在则创建.

    [ -f  ${DOWNLOAD_DIR} ] && { echo $(date +"%F %H:%M:%S") [ERROR] ${DOWNLOAD_DIR}是一个文件，请重新指定下载目录.>> ${LOG_DIR};exit 10; }
    [ -d ${DOWNLOAD_DIR} ]|| mkdir ${DOWNLOAD_DIR}
    [ $? -eq 0 ]|| { echo $(date +"%F %H:%M:%S") [ERROR] ${DOWNLOAD_DIR}创建失败，请检查权限配置.>> ${LOG_DIR};exit 11; }

    # 判断/usr/local目录下是否存在 ${LINK}目录或者文件
    [ -d ${LINK} -o -f ${LINK} ] && { echo $(date +"%F %H:%M:%S") [ERROR] ${LINK}是一个目录或者文件，请处理后重新安装 >> ${LOG_DIR};exit 12; }

    # 下载压缩包
    # 1.检查是否存在压缩包,存在则重命名。
    [ -f ${DOWNLOAD_DIR}/${ZK_FILE} ]&& {mv ${DOWNLOAD_DIR}/${ZK_FILE} ${DOWNLOAD_DIR}/${ZK_FILE}_$(date +"%F %H:%M:%S")}
    cd ${DOWNLOAD_DIR} && curl -o ${ZK_FILE}  ${DOWNLOAD_URL}
    # 2. 解压压缩包
    tar xf ${ZK_FILE} -C ${INSTALL_DIR}
    [ $? -eq 0 ]||{ echo $(date +"%F %H:%M:%S") [ERROR]  ${ZK_FILE}下载失败. >> ${LOG_DIR};exit 14;}

    # 创建软连接
    ln -sv ${INSTALL_DIR}/${ZK_DIR} ${LINK}
    [ $? -eq 0 ]||{ echo $(date +"%F %H:%M:%S") [ERROR] ${LINK}软链接创失败. >>  ${LOG_DIR};exit 13; }

    # 修改安装目录为${RUN_USER}用户
    chown -R ${RUN_USER}:${RUN_USER} ${INSTALL_DIR}/${ZK_FILE}
   
    echo "export PATH=\$PATH:${LINK}/bin" > /etc/profile.d/zk.sh
    source /etc/profile
    echo "zookeeper服务安装完成，若环境变未生效，请执行'srouce /etc/profile命令.'"
}

function zk_config() {
    vars=`echo $var|tr " ," " "`

    zk_conf_dir=${LINK}/conf
    zk_conf=${zk_conf_dir=}/zoo.cfg
    mkdir -p ${LINK}/data
    # cp ${zk_conf_dir}/zoo_sample.cfg ${zk_conf}
    cat > ${zk_conf} << EOF
clientPort=2181
maxSessionTimeout=360000
tickTime=2000
initLimit=10
syncLimit=30
dataDir=${LINK}/data
EOF
    for v in ${vars};do
        echo "3,,,, $v"
        ip=`echo $v|awk -F ':' '{print $1}'`
        echo $ip
        id=`echo $v|awk -F ':' '{print $2}'`
        echo $id
        if [ $host_ip == $ip ];then
            echo "host_ip==ip"
            my_id=`echo $v|awk -F ':' '{print $2}'`
            echo "${my_id}" > ${LINK}/data/myid
        fi;

        echo "server.${id}=${ip}:2888:3888" >> ${zk_conf}
    done;
    chown -R ${RUN_USER}:${RUN_USER} ${LINK}/
}

var=$1
echo "1,,,,$var"
host_ip=$(ifconfig|grep 'inet'|grep -v '127.0.0.*'|awk '{print $2}')

zk_install
zk_config $var

